<?php

namespace app\merchant\controller;

use app\common\controller\Tools;
use app\common\model\GoodsDetails as GoodsDetailsModel;
use app\common\model\Goods;
class GoodsDetails extends Common 
{
	// 商品详情模板的添加
	public function create()
	{
		return view('create');
	}
	public function insert()
	{
		$tool = new Tools;
		$data = $_POST; 
	    $arr=[];
		$arr['image']=$_POST['goods_details_content'];
		$arr['text']=$_POST['goods_details_text'];
	    $arrs[] = $arr;
		$insertData['goods_details_name']=$_POST['goods_details_name'];
		 
		$insertData['goods_details_content']=json_encode($arrs, JSON_UNESCAPED_UNICODE);
		
		return $tool->insert('goods_details', $insertData, 'insert', 'isPost');
	}


	// 商品详情模板数据列表
	public function showlist()
	{
		// 用户列表需要商户的id
		$data = GoodsDetailsModel::showlist();
		$pageString = $data->render();
		$this->assign([
			'data' => $data,
			'pageString' => $pageString,
		]);
		  
		return view('showlist');
	}

	// 刷新商品详情模板
	public function refresh()
	{
		$data = GoodsDetailsModel::refresh();
		if($data)
		{
			return returnJson(1, '刷新成功', $data);
		}else
		{
			return returnJson(1, '刷新失败，请稍后重试！');
		}
	}

	public function update()
	{
		$id = input('param.id');
		$data = GoodsDetailsModel::getDetail($id);
		$imgRootPath = config('imgRootPath');
		 
		 $newData=json_decode($data['goods_details_content'],true);
		 $data['goods_details_image']=$newData[0]['image'];
	    $data['goods_details_content']=$imgRootPath.'/goods/details/'.$newData[0]['image'];
	  
		$data['goods_details_text']=$newData[0]['text'];
	  
		$this->assign('data', $data);
		if(request()->isPost())
		{
			$info = input('param.');
			$model = new GoodsDetailsModel;
			$validate = $model->intValidate();
            if(!($validate->scene('update')->check($info)))
                  return returnJson(0, $validate->getError());
			  
			  
			  
			  
            $arr['image']=$info['goods_details_content'];
		    $arr['text']=$info['goods_details_text'];
		    $arrs[] = $arr;
		    $info['goods_details_content']=json_encode($arrs, JSON_UNESCAPED_UNICODE);
            $result = $model->edit($info['goods_details_id'], $info['goods_details_name'], $info['goods_details_content']);
			

            if($result !== false)
			{
				return returnJson(1, '修改成功');
			}else
			{
				return returnJson(0, '修改失败，请稍后重试！');
			}
		}
		return view('update');
	}
	public function delete()
	{
		$id = input('id');
		$result = model('goods_details')->changeDel($id);
		if(!$result)
			return returnJson(0, '删除失败，请稍后重试');

		return returnJson(1, '删除成功');
	}
	
	public function upload_img()
	{
		// 店铺编号+商户编号+时间戳+随机4位数
		$whereName = 'goods/details';
		$width = 750;
		$height = 1000;
		$data = Tools::upload_goods_img($whereName,$width, $height);
		//batch_upload_img($whereName);
	    //$filename = $this->batch_upload_img($whereName);
		 //return  $filename;
		return returnJson(1, '上传成功', $data);
	}
     public function batch_upload_img($whereName)
	 {
         $imgpath = config('img_path');
         $imgRootPath = config('imgRootPath'); 
	     $storeId = Goods::getStoreId();
	     $merchantId = session('merchant.merchant_id');
	     $old_filename = $storeId . $merchantId . time();
 	    
	 	$uploaddir = config('img_path') . $whereName . '/';
	 	$name = $_FILES['file']['name'];
	     
	 	$type = strtolower(substr(strrchr($name, '.'), 1));
	 	//获取文件类型
	 	if(!is_dir($uploaddir))
	     {
	 	   mkdir($uploaddir);
	     }
		$typeArr = array("jpg","png","gif");
	 	if (!in_array($type, $typeArr)) {
	 		return returnJson(0, '请上传jpg,png或gif类型的图片！');
	 	}
	 	$filename = $storeId . $merchantId . time() . mt_rand(1000, 9999);
        move_uploaded_file($_FILES['file']['tmp_name'], $uploaddir . $filename . '.' . $type) ;
		return $filename. '.' . $type;
	 }


    // public static function upload_goods_img1($whereName, $width="", $height="")
	// {
	// 	// $column = store_face_img_130_130
	// 	// 打开图片的相对路径
	// 	$imgpath = config('img_path');
	// 	// 绝对路径
	// 	$imgRootPath = config('imgRootPath');
	// 	$storeId = Goods::getStoreId();
	// 	$merchantId = session('merchant.merchant_id');
	// 	$old_filename = $storeId . $merchantId . time();
 	// 	$filename = $storeId . $merchantId . time() . mt_rand(1000, 9999);
	// 	$type = Goods::upload($whereName, $old_filename);
	// }  
	
    // public static function upload1($whereName, $filename, $thumb=[])
	// {
	// 	//$rootpath = ROOT_PATH;
	// 	//$uploaddir = str_replace('/www/', '', $rootpath);
	// 	$uploaddir = config('img_path') . $whereName . '/';
	// 	// halt($uploaddir);
	// 	$name = $_FILES['file']['name'];
	// 	$uploadfile = $uploaddir . $name;
	// 	$type = strtolower(substr(strrchr($name, '.'), 1));
	// 	//获取文件类型
		 
	//     if(!is_dir($uploaddir))
	//     {
	// 	   mkdir($uploaddir);
	//     }
		 
	// 	$typeArr = array("jpg","png","gif");
	// 	if (!in_array($type, $typeArr)) {
	// 		return returnJson(0, '请上传jpg,png或gif类型的图片！');
	// 	}
	// 	if (move_uploaded_file($_FILES['file']['tmp_name'], $uploaddir . $filename . '.' . $type)) 
	// 	{
	// 		return $type;
	// 	}else{
	// 		return false;
	// 	} 
	// }


}